package com.yueka.service.Impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yueka.mapper.MusicScoreMapper;
import com.yueka.pojo.entity.base.MusicScore;
import com.yueka.pojo.param.MusicScorePageParam;
import com.yueka.pojo.param.MusicScoreSaveParam;
import com.yueka.pojo.vo.MusicScoreVO;
import com.yueka.service.BaseFileService;
import com.yueka.service.MusicScoreService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 曲谱表 服务实现类
 * </p>
 *
 * @author D&M
 * @since 2025-10-20
 */
@Slf4j
@Service
public class MusicScoreServiceImpl extends ServiceImpl<MusicScoreMapper, MusicScore> implements MusicScoreService {
    @Resource
    BaseFileService baseFileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveScore(MusicScoreSaveParam param) {
        MusicScore musicScore = new MusicScore();
        BeanUtils.copyProperties(param, musicScore);
        saveOrUpdate(musicScore);
    }

    @Override
    public MusicScoreVO detail(Integer id) {
        log.info("------ 查询曲谱详情 >> 入参：{}", id);
        MusicScoreVO musicScoreVO = new MusicScoreVO();
        MusicScore entity = getById(id);
        BeanUtils.copyProperties(entity, musicScoreVO);
        return musicScoreVO;
    }

    @Override
    public void delete(Integer id) {
        log.info("------ 删除曲谱 >> 入参：{}", id);
        MusicScore entity = getById(id);
        entity.setDelFlag(1);
        updateById(entity);
    }

    @Override
    public Page<MusicScore> pageList(MusicScorePageParam param) {
        log.info("------ 查询曲谱分页列表 >> 入参：{}", param);
        return page(new Page<>(param.getPageNum(), param.getPageSize()),
                Wrappers.lambdaQuery(MusicScore.class)
                        .eq(MusicScore::getDelFlag, 0)
                        .like(ObjectUtil.isNotEmpty(param.getName()), MusicScore::getName, param.getName())
                        .eq(ObjectUtil.isNotEmpty(param.getType()), MusicScore::getType, param.getType()));
    }
}
